<?php

if (!defined('IN_XLP')) {
    exit('Access Denied');
}

/**
 * Description of menuAction
 * 区服管理
 * @author skyinter
 */
class gameareaAction extends commonAction {

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
    }



    /**
     * 商品规格
     */
    public function index() {
        //游戏
        $games = M('game')->findAll();
        //服信息
        $game_server = M('game_server')->findAll();
        //区信息
        $page    = $this->_get('page') ? $this->_get('page') : '1';
        $game_id = $this->_getid('game_id' , 0);
        $where = '1=1';
        if($game_id) {
            $where .= " AND game_id = ".$game_id;
        } 
        $game_area = M('game_area')->where($where)->page($page , 20)->order('`sort`')->findAll();
        $total = M('game_area')->where($where)->count();    
        if (count($game_area)) {
            foreach ($game_area as $k => &$v) {
                foreach ($games as $val) {
                    if($val['id'] === $v['game_id']){
                        $v['game'] = $val['name'];
                    }
                }

                $game_area[$k]['server'] = array();
                if (!count($game_server))
                    break;
                foreach ($game_server as $key => $value) {
                    if ($value['area_id'] == $v['id']) {
                        $game_area[$k]['server'][] = $value;
                        unset($game_server[$key]);
                    }
                }
            }
        }
        // z($game_area);
        $this->assign('game_area', $game_area);
        $this->assign('page', $page);
        $this->assign('total', $total);
        $this->assign('games' , $games);
        $this->assign('gameid' , $game_id);
        $this->display();
    }

    /*
     * 添加商品规格
     */

    public function add() {

        if (isset($_POST['area_name'])) {
            $area_name = $this->_post('area_name');
            $area_sort = $this->_post('area_sort');
            $game_id   = $this->_postid('game_id');
            //检查规格是否已经存在
            $is_exist = M('game_area')->where(array('area_name' => $area_name,'game_id'=>$game_id))->find();
            if ($is_exist)
                sendJson(array('err' => 1, 'msg' => '该区已经存在'));

            //插入规格
            $area_id = M('game_area')->insert(array('game_id' => $game_id , 'area_name' => $area_name, 'sort' => $area_sort));

            if ($area_id) {
                $server_name = $this->_post('server_name');
                $server_num = count($server_name);
                //是否有规格值
                if ($server_num && $server_name[0]) {
                    //区编码
                    $abridge = $this->_post('abridge');
                    $cross   = $this->_post('cross_no');
                    $data = array();
                    for ($i = 0; $i < $server_num; $i++) {
                        $data[] = array(
                            'area_id' => $area_id,
                            'area_name' => $area_name,
                            'server_name' => $server_name[$i],
                            'abridge' => $abridge[$i],
                            'cross_no' => $cross[$i]
                        );
                    }
                    //插入规格值
                    $insert_num = M('game_server')->insertAll($data);

                    if ($insert_num != $server_num) {
                        sendJson(array('err' => 1, 'msg' => '部分服信息添加失败'));
                    } else {
                        sendJson(array('err' => 0, 'msg' => '区服信息添加成功'));
                    }
                }

                sendJson(array('err' => 0, 'msg' => '区服添加成功'));
            } else {
                sendJson(array('err' => 1, 'msg' => '区服添加失败'));
            }
        }
        $games = M('game')->findAll();
        $this->assign('games' , $games);
        $this->display();
    }

    /*
     * 添加商品规格值
     */

    public function server_value_add() {
        $server_id = $this->_post('server_id');
        $area_id = $this->_post('area_id');
        $area_name = $this->_post('area_name');
        $server_name = $this->_post('server_name');
        $abridge = $this->_post('abridge');
        $cross_no = $this->_post('cross_no');
        $area_sort = $this->_post('area_sort');

        if (trim($server_name) == '') {
            sendJson(array('err' => 1, 'msg' => '游戏服不能为空'));
        }

        //查找规格是否存在
        $is_exist = M('game_server')->where(
                        array(
                            'area_id' => $area_id,
                            'server_name' => $server_name,
                            'abridge' => $abridge)
                )->find();

        if ($is_exist)
            sendJson(array('err' => 1, 'msg' => '该游戏服已经存在'));

        //添加OR更新
        if ($server_id) {
            M('game_server')->update(array('server_name' => $server_name, 'abridge' => $abridge, 'cross_no' => $cross_no), array('id' => $server_id));
            sendJson(array('err' => 0, 'msg' => '区服更新成功'));
        } else {
            M('game_server')->insert(
                    array('area_id' => $area_id, 'area_name' => $area_name, 'server_name' => $server_name, 'abridge' => $abridge, 'cross_no' => $cross_no)
            );
            sendJson(array('err' => 0, 'msg' => '区服添加成功'));
        }
    }



    public function update_area_sort() {
        $sort    = $this->_post('sort');
        $where['id'] = $this->_postid('area_id');
        $result  = M('game_area')->update(array('sort' => $sort) , $where);
        if($result) {
            sendJson(array('err' => 0, 'msg' => '区排序更新成功'));
        }else{
            sendJson(array('err' => 1, 'msg' => '区排序更新失败'));
        }
    }


    public function ajax() {
        $id = $this->_get('id') ? $this->_get('id') : null;
        $type = $this->_get('type') ? $this->_get('type') : null;

        switch ($type) {
            case 'down': //商品下架
                $data = array('state' => 2);
                $stat = D('goods')->modify($id, $data);
                break;
            case 'del': //商品删除
                $stat = D('goods')->where(array('id'=>$id))->delete();
                break;
            case 'up': //商品上架
                $data = array('state' => 1);
                $stat = D('goods')->modify($id, $data);
                break;
            case 'cateSave': //商品类别名称更改
                $data = array('name' => $this->_get('name'));
                $stat = D('goodsCategory')->modify($id, $data);
                break;
            case 'cateDel': //分类
                $result = D('goods')->where(array('cid'=>$id))->find();
                if($result) {
                    $this->JsonReturn('该分类下有产品，无法删除', null , 0);
                }
                $stat = D('goodsCategory')->where(array('id'=>$id))->delete();
                break;
            case 'cdel':
                $stat = D('order')->where(array('id' => $id))->delete();
                break;    
            default:
                $stat = 0;
                break;
        }

        if ($stat > 0) {
            $info = '请求成功';
            $this->JsonReturn($info, null, 1);
        } else {
            $info = '请求失败';
            $this->JsonReturn($info, null, 0);
        }
    }

}
